<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("relationshipFunction.php");

$errMsg = "";
$entityPar = "";
$entityChi = "";
$descrParChi = "";
$descrChiPar = "";
$note = "";
$parentOptionality = 'N';
$childrenOptionality = 'Y';
$childrenMulteplicity = 'Y';
$integrityCheck = 'Y';
$cascadeDelete = 'N';
$number = 0;
$attributePar = "";
$attributeChi = "";	

$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$dataArea = "";
$sCondDataArea = "";
if(isset($_REQUEST["da"])){
	$dataArea = $_REQUEST["da"];
	if(!isset($aDataArea[$_REQUEST["da"]])){
		$errMsg .= _t("user disabled for data-area")." \"".$_REQUEST["da"]."\"";
	}
}else if(isset($_REQUEST["data_area"])){
	$dataArea = $_REQUEST["data_area"];
	if(!isset($aDataArea[$_REQUEST["data_area"]])){
		$errMsg .= _t("user disabled for data-area")." \"".$_REQUEST["data_area"]."\"";
	}
}else{
	$errMsg .= _t("data-area missing");
}

if(isset($_REQUEST["ent"]))
	$entityPar = str_replace("'", "''", stripslashes($_REQUEST["ent"]));
else if(isset($_REQUEST["ent"]))
	$entityPar = str_replace("'", "''", stripslashes($_REQUEST["ent"]));
		
if(isset($_REQUEST["actvar"])) {
	if ($_REQUEST["actvar"] == "save") {
		$errMsg .= relationshipUpd();
	} elseif ($_REQUEST["actvar"] == "delete") {
		$errMsg .= relationshipDel();
	} elseif ($_REQUEST["actvar"] == "cancel") {
		if ($_REQUEST['from_ins']=='si') {
			$errMsg .= relationshipDel();
		} else {
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);			
		}
	}
	$entityChi = str_replace("'", "''", stripslashes($_REQUEST["entityChi"]));
	$descrParChi = str_replace("'", "''", stripslashes($_REQUEST["descrParChi"]));
	$descrChiPar = str_replace("'", "''", stripslashes($_REQUEST["descrChiPar"]));
	$note = str_replace("'", "''", stripslashes($_REQUEST["note"]));
	$parentOptionality = sxREQUEST("parentOptionality");
	$childrenOptionality = sxREQUEST("childrenOptionality");
	$childrenMulteplicity = sxREQUEST("childrenMulteplicity");
	$integrityCheck = sxREQUEST("integrityCheck");
	$cascadeDelete = sxREQUEST("cascadeDelete");
	$number = str_replace("'", "''", stripslashes($_REQUEST["number"]));
	$aRelKey = array();
	foreach($_REQUEST as $nome => $val) {
		if (substr($nome,0,8)!='rel_key_') continue;
		$aRelKey[substr($nome,8)] = $val;
	}
} elseif(isset($_REQUEST["ent"]) && isset($_REQUEST["entityChi"]) && isset($_REQUEST["number"])){
	//carico i dati sulla relationship
	$sSQL = "SELECT r.*, a.attribute AS parent_attribute, rk.child_attribute FROM relationship r " .
			" LEFT JOIN attribute a ON a.entity = r.parent_entity AND a.primary_key <> 'N' " .
			" LEFT JOIN relationship_key rk ON r.parent_entity = rk.parent_entity " .
			"   AND r.child_entity = rk.child_entity " .
			"   AND r.number=rk.number " .
			"   AND rk.parent_attribute = a.attribute".
			" WHERE r.parent_entity = '".$entityPar."' " .
			"   AND r.child_entity = '".$_REQUEST["entityChi"]."' " .
			"   AND r.number = ".$_REQUEST["number"].
			" ORDER BY a.sequence, a.attribute";
	$objSDAO->query($sSQL,"sel");
	if($row = $objSDAO->fetch("sel")){
		$entityChi = $row["child_entity"];
		$descrParChi = $row["description_parent_child"];
		$descrChiPar = $row["description_child_parent"];
		$note = $row["note"];
		$parentOptionality = $row["parent_optionality"];
		$childrenOptionality = $row["children_optionality"];
		$childrenMulteplicity = $row["children_multeplicity"];
		$integrityCheck = $row["integrity_check"];
		$cascadeDelete = $row["cascade_delete"];
		$number = $db_number = $row["number"];
	}else{
		header("location:entityView.php?ent=".$_REQUEST['ent']."&xsn=".XSN);
		exit();
	}
	$aRelKey = array();
	while($row) {
		if (!empty($row['parent_attribute'])) {
			$aRelKey[$row['parent_attribute']] = $row['child_attribute'];
		}
		$row = $objSDAO->fetch("sel");	
	}
}else{
	$errMsg .= _t("unable to load relationship info");
}
// carica array attributi figlio
$aChildAttr = array();
$sSQL = "SELECT a.attribute FROM attribute a WHERE a.entity = '".$entityChi."' ORDER BY a.sequence";
$objSDAO->query($sSQL,"chiatt");
while($rec = $objSDAO->fetch("chiatt")) {
	$aChildAttr[] = $rec['attribute'];
}
//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function backModRel(){
		document.objFrm.actvar.value = 'cancel';
		document.objFrm.submit();
	}
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea = document.getElementById("messArea");
		var objEntChi = document.getElementById("entityChi");
		if(objEntChi.value == "" || objEntChi.value == 0){
			errMsg += "entity child is required<br>";
			objEntChi.className = "inpt01";
			objEntChi.focus();
		}else{
			objEntChi.className = "inpt01";
		}
		var objNumber = document.getElementById("number");
		if(objNumber.value == "" || objNumber.value == 0){
			errMsg += "number is required<br>";
			objNumber.className = "inpt01";
			objNumber.focus();
		}else{
			objNumber.className = "inpt01";
		}
		var objDescrParChi = document.getElementById("descrParChi");
		if(objDescrParChi.value == ""){
			errMsg += "Parent child description is required<br>";
			objDescrParChi.className = "inpt01";
			objDescrParChi.focus();
		}else{
			objDescrParChi.className = "inpt01";
		}
		var objDescrChiPar = document.getElementById("descrChiPar");
		if(objDescrChiPar.value == ""){
			errMsg += "Child parent description is required<br>";
			objDescrChiPar.className = "inpt01";
			objDescrChiPar.focus();
		}else{
			objDescrChiPar.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
//output comandi
$aCmd = array("insRel"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t(""),
										"text"=>_t("save"),
										"perm"=>"R"),
					"delRel"=>array("pag"=>"javascript:confirmDelete()",
										"title"=>_t(""),
										"text"=>_t("cancel"),
										"perm"=>"R"),
					"back"=>array("pag"=>$objUser->getHistory(0, false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0),
										"perm"=>"M"),
					"back"=>array("pag"=>"javascript:backModRel()",
										"title"=>_t("cancel operation"),
										"text"=>_t("cancel"),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"relationshipMod.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"from_ins\" value=\"".((isset($_REQUEST['from_ins']))?$_REQUEST['from_ins']:'no')."\">");
echo("<input type=\"hidden\" name=\"actvar\" id=\"actvar\" value=\"save\">");
echo("<input type=\"hidden\" name=\"ent\" id=\"ent\" value=\"$entityPar\">");
echo("<input type=\"hidden\" name=\"data_area\" id=\"data_area\" value=\"$dataArea\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"entityPar\" id=\"entityPar\" value=\"$entityPar\">");
echo("<input type=\"hidden\" name=\"entityChi\" id=\"entityChi\" value=\"$entityChi\">");
echo("<input type=\"hidden\" name=\"db_number\" id=\"db_number\" value=\"$number\">");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\"\" colspan=\"6\">"._t("Relationship")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("Parent entity")."</td>" .
			"<td class=\"text01\" width=\"25%\">$entityPar</td>" .
			"<td class=\"labl01\" width=\"15%\">"._t("Child entity")."</td>" .
			"<td class=\"text01\" width=\"30%\">$entityChi</td>" .
			"<td class=\"labl01\" width=\"5%\">"._t("Number")."</td>" .
			"<td class=\"text01\" width=\"5%\">" .
			"<input type='text' class='inpt01' name='number' id='number' value=\"$number\" size='3'></td>" .
		"</tr>\n");
foreach($aRelKey as $parentAttr => $childAttr) {
	echo("<tr>" .
				"<td class=\"labl01\">"._t("Parent key")."</td>" .
				"<td class=\"text01\">".$parentAttr."</td>" .
				"<td class=\"labl01\">"._t("Child attribute")."</td>" .
				"<td class=\"text01\" colspan='3'>\n".
				"<SELECT name='rel_key_".$parentAttr."' class='inpt01' >\n" .
				"<OPTION value=''>-- seleziona un attribute --</OPTION>");
	foreach($aChildAttr as $kk => $attr) {
		$sel = ($aRelKey[$parentAttr]==$attr)?' SELECTED':'';
		echo("<OPTION value=\"$attr\"$sel>$attr</OPTION>");
	}
	echo("</SELECT></td>\n</tr>\n");
}
echo("<tr>" .
			"<td class=\"labl01\">"._t("Parent-child descr.")."*</td>" .
			"<td class=\"text01\" colspan=\"5\"><input class=\"inpt01\" type=\"text\" name=\"descrParChi\" id=\"descrParChi\" maxlength=\"80\" size=\"80\" value=\"".sxHE($descrParChi)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Child-parent descr.")."*</td>" .
			"<td class=\"text01\" colspan=\"5\"><input class=\"inpt01\" type=\"text\" name=\"descrChiPar\" id=\"descrChiPar\" maxlength=\"80\" size=\"80\" value=\"".sxHE($descrChiPar)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\" colspan=\"5\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
$chkY = ($parentOptionality == 'Y')?"CHECKED":"";
$chkN = ($parentOptionality == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Parent optionality")."</td>" .
			"<td class=\"text01\" colspan=\"5\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"parentOptionality\" id=\"parentOptionality\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"parentOptionality\" id=\"parentOptionality\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($childrenOptionality == 'Y')?"CHECKED":"";
$chkN = ($childrenOptionality == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Children optionality")."</td>" .
			"<td class=\"text01\" colspan=\"5\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"childrenOptionality\" id=\"childrenOptionality\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"childrenOptionality\" id=\"childrenOptionality\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($childrenMulteplicity == 'Y')?"CHECKED":"";
$chkN = ($childrenMulteplicity == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Children multiplicity")."</td>" .
			"<td class=\"text01\" colspan=\"5\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"childrenMulteplicity\" id=\"childrenMulteplicity\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"childrenMulteplicity\" id=\"childrenMulteplicity\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($integrityCheck == 'Y')?"CHECKED":"";
$chkN = ($integrityCheck == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Referential integriti")."</td>" .
			"<td class=\"text01\" colspan=\"5\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"integrityCheck\" id=\"integrityCheck\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"integrityCheck\" id=\"integrityCheck\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($cascadeDelete == 'Y')?"CHECKED":"";
$chkN = ($cascadeDelete == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Cascade delete")."</td>" .
			"<td class=\"text01\" colspan=\"5\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"cascadeDelete\" id=\"cascadeDelete\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"cascadeDelete\" id=\"cascadeDelete\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
echo("</table>");
echo("</form>");
echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
